﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace MapEditor
{
    public abstract partial class ObjectBase : UserControl
    {
        public string Description = Consts.Unknown;
        public string ObjectName = Consts.Unknown;
        public bool IsWalkable = true;
        public int BaseObjectID = Consts.None;
        
        public ObjectBase(double x, double y, int baseObjID)
        {
            InitializeComponent();

            BaseObjectID = baseObjID;

            this.SetValue(Canvas.LeftProperty, x);
            this.SetValue(Canvas.TopProperty, y);
        }

        public void SetWidth(double width)
        {
            this.Width = width;
            SelectionRect.Width = width;
            CollisionRect.Width = width;
            ObjClip.Rect = new Rect(0,0,this.Width,this.Height);
        }

        public void SetHeight(double height)
        {
            this.Height = height;
            SelectionRect.Height = height;
            CollisionRect.Height = height;
            ObjClip.Rect = new Rect(0, 0, this.Width, this.Height);
        }

        internal bool DetectCollision(Point pt, ref Point selectionPoint)
        {
            double x = (double)this.GetValue(Canvas.LeftProperty);
            double y = (double)this.GetValue(Canvas.TopProperty);
            
            x += (double)CollisionRect.GetValue(Canvas.TopProperty);
            y += (double)CollisionRect.GetValue(Canvas.LeftProperty);

            bool collide = pt.X >= x && pt.Y >= y && pt.X < (x + CollisionRect.ActualWidth) && pt.Y < (y + CollisionRect.ActualHeight);

            if (true == collide)
            {
                selectionPoint.X = pt.X-x;
                selectionPoint.Y = pt.Y-y;
            }
            return collide;
        }

        public abstract ObjectBase Clone(int maxRadius);
        public abstract void Deselect();
        public abstract ObjectBase Select();
    }
}
